package com.rjwh.dingdong.client.activity.loginregist;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.aishua.lib.utils.AsStrUtils;
import com.rjwh.dingdong.client.R;
import com.rjwh.dingdong.client.activity.BaseActivity;
import com.rjwh.dingdong.client.mvp.presenter.impl.ThirdPartyLoginBindPhoneNumberPresenter;
import com.rjwh.dingdong.client.mvp.view.ThirdPartyLoginBindPhoneNumberView;
import com.rjwh.dingdong.client.util.ContactPhoneUtils;
import com.rjwh.dingdong.client.util.ToastUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author Kay
 * @ClassName ThirdPartyLoginBindPhoneNumberActivity
 * @Description
 * @date 2016-10-13
 * @history 2016-10-13 author: description:
 */
public class ThirdPartyLoginBindPhoneNumberActivity extends BaseActivity implements ThirdPartyLoginBindPhoneNumberView {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.third_party_login_bind_phone_number_mobile_number)
    EditText phoneNoEt;
    @BindView(R.id.third_party_login_bind_phone_number_get_code_btn)
    Button getCodeBtn;
    @BindView(R.id.third_party_login_bind_phone_number_verification_code)
    EditText codeInputEt;
    @BindView(R.id.third_party_login_bind_phone_number_finsh_btn)
    Button finshBtn;
    private TimeCount time;

    private ThirdPartyLoginBindPhoneNumberPresenter presenter;
    private String uid;
    private String nickname;
    private String avatar;
    private String apptype;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.third_party_login_bind_phone_number_layout);
        ButterKnife.bind(this);
        time = new TimeCount(10 * 6000, 1000);// 构造CountDownTimer对象
        parseIntent();
        initView();
        presenter = new ThirdPartyLoginBindPhoneNumberPresenter(this, this);
    }

    private void parseIntent() {
        Intent intent = getIntent();
        if (intent != null) {
            uid = intent.getStringExtra("uid");
            nickname = intent.getStringExtra("nickname");
            avatar = intent.getStringExtra("avatar");
            apptype = intent.getStringExtra("apptype");
        }
    }

    private void initView() {
        initToolbarHelper();
        getCodeBtn.setClickable(false);
        if (ContactPhoneUtils.isMobileNO(phoneNoEt.getText().toString()) && !AsStrUtils.isEmpty(codeInputEt.getText().toString())) {
            finshBtn.setBackgroundResource(R.drawable.long_btn);
            finshBtn.setClickable(true);
        } else {
            finshBtn.setBackgroundResource(R.drawable.long_btn_unable);
            finshBtn.setClickable(false);
        }
        phoneNoEt.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                updateLoginBtnState();
                updateCodeBtnState();
            }
        });
        codeInputEt.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                updateLoginBtnState();
            }
        });
    }

    private void updateLoginBtnState() {
        if (!ContactPhoneUtils.isMobileNO(phoneNoEt.getText().toString()) || AsStrUtils.isEmpty(codeInputEt.getText().toString())) {
            finshBtn.setBackgroundResource(R.drawable.long_btn_unable);
            finshBtn.setClickable(false);
        } else {
            finshBtn.setBackgroundResource(R.drawable.long_btn);
            finshBtn.setClickable(true);
        }
    }


    private void updateCodeBtnState() {
        if (!ContactPhoneUtils.isMobileNO(phoneNoEt.getText().toString())) {
            getCodeBtn.setClickable(false);
            getCodeBtn.setBackgroundResource(R.drawable.gray_white_corner_normal);
            getCodeBtn.setTextColor(getResources().getColor(R.color.content_hint_text_color));
        } else {
            getCodeBtn.setBackgroundResource(R.drawable.gray_white_corner_pressed);
            getCodeBtn.setClickable(true);
            getCodeBtn.setTextColor(getResources().getColor(R.color.blue_button_nor));
        }
    }

    private void initToolbarHelper() {
        this.setSupportActionBar(this.toolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setTitle("绑定手机");
        }
        getSupportActionBar().setHomeButtonEnabled(true); //设置返回键可用
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
    }

    @OnClick({R.id.third_party_login_bind_phone_number_get_code_btn, R.id.third_party_login_bind_phone_number_finsh_btn})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.third_party_login_bind_phone_number_get_code_btn:
                String inputPhone1 = phoneNoEt.getText().toString();
                doGetCode(inputPhone1);
                break;
            case R.id.third_party_login_bind_phone_number_finsh_btn:
                String inputPhone2 = phoneNoEt.getText().toString();
                String codeInput = codeInputEt.getText().toString();
                if (AsStrUtils.isEmpty(inputPhone2) || AsStrUtils.isEmpty(codeInput)) {
                    ToastUtil.showToast(this, "请先获取验证码！");
                    return;
                }
                doNextStep(inputPhone2, codeInput);
                break;
        }
    }

    private void doNextStep(String inputPhone2, String codeInput) {
        presenter.doUserOuterSysBind(inputPhone2, codeInput, uid, nickname, avatar, apptype);
    }

    private void doGetCode(String inputPhone1) {
        presenter.doGetCode(inputPhone1);
    }

    @Override
    public void showLoading() {
        showLoadDialog();
    }

    @Override
    public void dismissLoading() {
        dismissLoadDialog();
    }

    @Override
    public void loadData(boolean isSuccess) {
        if (isSuccess) {
            setResult(RESULT_OK);
        }
        finish();
    }

    @Override
    public void setTimerData() {
        time.start();
    }

    class TimeCount extends CountDownTimer {

        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);// 参数依次为总时长,和计时的时间间隔
        }

        @Override
        public void onFinish() {// 计时完毕时触发
            getCodeBtn.setText("获取验证码");
            updateCodeBtnState();
        }

        @Override
        public void onTick(long millisUntilFinished) {// 计时过程显示
            getCodeBtn.setClickable(false);
            getCodeBtn.setBackgroundResource(R.drawable.gray_white_corner_normal);
            getCodeBtn.setText(millisUntilFinished / 1000 + "秒后重试");
            getCodeBtn.setTextColor(getResources().getColor(R.color.content_hint_text_color));
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        presenter.detachView();
        time.cancel();
    }
}
